<template>
  <div class="bpm-form">
    <a-spin :spinning="confirmLoading>0">
      <bpm-button :father="this" @save="save" ref="bpmButton" @afterBpmRefresh="afterBpmRefresh"></bpm-button>
      <a-card id="formContent" size="small" class="bpm-module-card">
        <template slot="title">
          <span class="bpm-module-title">{{$t('m.eform.appform.common.business_form_title')}}</span>
        </template>
        <!--<a href="#" slot="extra">more</a>-->
        <k-form-build @refreshParent="refreshParent" @close="close" :value="jsonData" @submit="handleSubmit" ref="KFormBuild" :allDataSource="allDataSource"
                      @refreshAllData="refreshAllData" :dsCode="dsCode" :stores="stores"
                      @addModelKey="addModelKey" :dictMap="dictMap" :elValue="elValue" :formId="id" :fks="fks" :uploadTimestamp="uploadTimestamp"/>
      </a-card>
      <br/>
      <a-card id="bpmIdea" size="small" class="bpm-module-card">
        <template slot="title">
          <span class="bpm-module-title">{{$t('m.eform.appform.common.approval_opinions_title')}}</span>
        </template>
        <!--<a href="#" slot="extra">more</a>-->
        <bpm-idea :process-instance-id="processInstanceId" :refreshFlg="refreshProcess"></bpm-idea>
      </a-card>
      <br/>
      <a-card id="bpmPic" size="small" class="bpm-module-card">
        <template slot="title">
          <span class="bpm-module-title">{{$t('m.eform.appform.common.process_chart_title')}}</span>
        </template>
        <!--<a href="#" slot="extra">more</a>-->
        <bpm-pic :process-instance-id="processInstanceId"
                 :process-def-id="processDefId"
                 :refreshFlg="refreshProcess"
        ></bpm-pic>
      </a-card>
    </a-spin>
  </div>
</template>

<script>

  import {getAction} from '@/api/manage'
  import YchConstant from "@/api/YchConstant";
  import BpmButton from '../../bpm/modules/BpmButton'
  import { BpmMixin } from '@/mixins/BpmMixin'
  import { FormMixin } from './FormMixin'
  import { KFormBuild } from '../modules/designer/packages'
  import BpmPic from '../../bpm/modules/bpmTrack/BpmPic'
  import BpmIdea from '../../bpm/modules/bpmTrack/BpmIdea'

  export default {
    name: 'BpmForm',
    mixins: [BpmMixin, FormMixin],
    components: {
      BpmButton,
      KFormBuild,
      BpmPic,
      BpmIdea
    },
    props: {
      fks: {
        type: Array,
        default: () => []
      },
      parentValue: {
        type: Object,
        default: () => {}
      },
      stores: {
        type: Object,
        default: () => {
          return {}
        }
      },
      relationList: Array,
      refreshMap: Object,
      tabConfig: Object,
      relationStatus: String,
      relationOpenFks: String,
    },
    provide() {
      return {
        getBpmButtonVm: this.getBpmButtonVm,
      }
    },
    data () {
      return {
        isFlow: true,
        tableName: null,
        getTableNameByFormCode: YchConstant.PREFIX_EFORM + '/engine/formEngine/getTableNameByFormCode',
        relationValue: null,
      }
    },
    methods: {
      getBpmButtonVm() {
        return this.$refs.bpmButton
      },
      editableModel(jsonList) {
        if (!jsonList) {
          return
        }
        jsonList.forEach(vo => {
          if (vo.options && typeof vo.options.disabled !== "undefined" && typeof vo.options.disabledBak === "undefined") {
            // 备份原始数据
            vo.options.disabledBak = vo.options.disabled
          }
          if (this.startPage && vo.options && typeof vo.options.disabled !== "undefined") {
            // 未启动时按表单默认配置
            vo.options.disabled = vo.options.disabledBak
          } else if (vo.dataId) {
            vo.options.disabled = !(this.editableList.indexOf(vo.dataId) !== -1)
          } else if (vo.model) {
            vo.options.disabled = !(this.editableList.indexOf(vo.model) !== -1)
          } else if (vo.type === 'editableTable' && vo.tableName) {
            vo.options.disabled = !(this.editableList.indexOf(vo.tableName) !== -1)
          } else {
            this.editableModel(vo.list)
            this.editableModel(vo.trs)
            this.editableModel(vo.tds)
            this.editableModel(vo.columns)
            this.editableModel(vo.tabs)
          }
        })
      },
      requiredModel(jsonList) {
        if (!jsonList) {
          return
        }
        jsonList.forEach(vo => {
          if (vo.rules && typeof vo.rulesBak === "undefined") {
            // 备份原始数据
            vo.rulesBak = vo.rules
          }
          if (this.startPage && vo.rules) {
            // 未启动时按表单默认配置
            vo.rules = vo.rulesBak
          } else if (vo.dataId && vo.rules) {
            const requiredRule = vo.rules.find(item => typeof item.required !== "undefined");
            if (requiredRule) {
              requiredRule.message = requiredRule.message || this.$t('m.common.tip.verify_required')
              requiredRule.required = this.requiredList.indexOf(vo.dataId) !== -1
            }
          } else if (vo.model && vo.rules) {
            const requiredRule = vo.rules.find(item => typeof item.required !== "undefined");
            if (requiredRule) {
              requiredRule.message = requiredRule.message || this.$t('m.common.tip.verify_required')
              requiredRule.required = this.requiredList.indexOf(vo.model) !== -1
            }
          } else if (vo.type === 'editableTable' && vo.tableName && vo.rules) {
            const requiredRule = vo.rules.find(item => typeof item.required !== "undefined");
            if (requiredRule) {
              requiredRule.message = requiredRule.message || this.$t('m.common.tip.verify_required')
              requiredRule.required = this.requiredList.indexOf(vo.tableName) !== -1
            }
          } else {
            this.requiredModel(vo.list)
            this.requiredModel(vo.trs)
            this.requiredModel(vo.tds)
            this.requiredModel(vo.columns)
            this.requiredModel(vo.tabs)
          }
        })
      },
      hiddenBpmModel(jsonList) {
        if (!jsonList) {
          return
        }
        jsonList.forEach(vo => {
          if (vo.options && typeof vo.options.hidden !== "undefined" && typeof vo.options.hiddenBak === "undefined") {
            // 备份原始数据
            vo.options.hiddenBak = vo.options.hidden
          }
          if (this.startPage && vo.options && typeof vo.options.hidden !== "undefined") {
            // 未启动时按表单默认配置
            vo.options.hidden = vo.options.hiddenBak
          } else if (vo.dataId) {
            vo.options.hidden = this.hiddenList.indexOf(vo.dataId) !== -1 || vo.options.hiddenBak
          } else if (vo.model) {
            vo.options.hidden = this.hiddenList.indexOf(vo.model) !== -1 || vo.options.hiddenBak
          } else if (vo.type === 'editableTable' && vo.tableName) {
            vo.options.hidden = this.hiddenList.indexOf(vo.tableName) !== -1 || vo.options.hiddenBak
          } else {
            this.hiddenBpmModel(vo.list)
            this.hiddenBpmModel(vo.trs)
            this.hiddenBpmModel(vo.tds)
            this.hiddenBpmModel(vo.columns)
            this.hiddenBpmModel(vo.tabs)
          }
        })
      },
      refreshAllData(){
        // 重新加载
        this.detail()
      },
      setPk() {
        if (this.formValues && this.tableName) {
          this.$emit('setPk', {
            record: this.formValues,
            tableName: this.tableName,
            relationStatus: this.relationStatus,
            isForm: true,
          })
        }
      },
    },
    created() {
      getAction(this.getTableNameByFormCode, {formCode: this.formCode}).then((res) => {
        if (res.success) {
          this.tableName = res.result
        }
      })

      // 把当前对象放入stores，方便其他对象使用
      if (!this.stores.$vm) {
        this.stores.$vm = {}
      }
      this.stores.$vm[this.formCode] = this
    },
    watch: {
      jsonData: {
        handler (value) {
          this.editableModel(value.list)
          this.requiredModel(value.list)
          this.hiddenBpmModel(value.list)
        },
        immediate: true
      },
      editableList: {
        handler () {
          this.editableModel(this.jsonData.list)
        },
        immediate: true
      },
      requiredList: {
        handler () {
          this.requiredModel(this.jsonData.list)
        },
        immediate: true
      },
      hiddenList: {
        handler () {
          this.hiddenBpmModel(this.jsonData.list)
        },
        immediate: true
      },
    }
  }
</script>
<style lang="less">
  .bpm-form {
    .bpm-module-card{
      margin: 0px 20px;
      .bpm-module-title {
        font-size: 16px;
        font-weight: 600;
      }
    }
  }
</style>

